//
//  TWDetailCollectionView.swift
//  EyepetizerImitative
//
//  Created by 田伟 on 2017/3/13.
//  Copyright © 2017年 个人. All rights reserved.
//

import UIKit

class TWDetailCollectionView: UICollectionView {

    // MARK:- 属性
    var videosArray : [TWItemModel]?{
        didSet {
            guard let videosArray = videosArray else {
                return
            }
            modelArray = videosArray
            reloadData()
        }
    }
    var modelArray = [TWItemModel]()
    var flowLayout = UICollectionViewFlowLayout()
    
    var fromWhichVc : String = String()
    
    // MARK:- 系统回调
    override func awakeFromNib() {
        super.awakeFromNib()
        initCollectionView()
    }
    
    private func initCollectionView(){
        // 初始化布局
        flowLayout.minimumInteritemSpacing = 2
        flowLayout.scrollDirection = .horizontal
        // 初始化CollectionView
        collectionViewLayout = flowLayout
        showsHorizontalScrollIndicator = false
        delegate = self
        dataSource = self
        register(UINib(nibName: "TWDetailCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: DetailCollectionViewCell)
    }
}

extension TWDetailCollectionView : UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return modelArray.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DetailCollectionViewCell, for: indexPath) as! TWDetailCollectionViewCell
        cell.model = modelArray[indexPath.item]
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let type = modelArray[indexPath.item].dataType!
        if type == "VideoBeanForClient" {
            return CGSize(width: collectionCellWidth, height: collectionCellHeight)
        }
        return CGSize(width: collectionCellHeight, height: collectionCellHeight)
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let selectedModel = modelArray[indexPath.row]
        let type = selectedModel.dataType!
        var dict = [String : AnyObject]()
        dict[fromWhichVc] = selectedModel
        if type == VideoBeanForClient {
            // 发送通知
//            NotificationCenter.default.post(name: NSNotification.Name(rawValue: DetailCollectionViewCellDidClickNotification), object: selectedModel)
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: DetailCollectionViewCellDidClickNotification), object: dict)
        } else if type == ActionCard {
            print("查看全部")
        }
    }
}

